package com.yipin.neimeng.constant;





import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpClient {
    public static int executeMethod(HttpMethod method) throws IOException {
        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) new URL(method.getUrl()).openConnection();
            connection.setRequestMethod(method.getName());
            connection.setDoOutput(method.permitsRequestBody());

            for (Map.Entry<String, String> header : method.getRequestHeaders().entrySet()) {
                connection.setRequestProperty(header.getKey(), header.getValue());
            }

            if (method.requiresRequestBody()) {
                connection.setDoOutput(true);
                try (OutputStream out = connection.getOutputStream()) {
                    method.writeRequestBody(out);
                }
            }

            int responseCode = connection.getResponseCode();
            // ... 读取响应 ...
            return responseCode;
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

}
